package com.zjkj.mp.controller.messageManage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zjkj.mp.service.customer.GroupService;
import com.zjkj.mp.utils.Global;
import com.zjkj.mp.utils.PaginatedResult;
import com.zjkj.mp.utils.Toolkit;
import com.zjkj.mp.utils.security.SpringSecurityUtils;
import com.zjkj.mp.vo.customer.Group;
import com.zjkj.mp.vo.user.MpUser;

/**
 * 
 * 短信分组控制器
 *
 * @author 陈晨
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2013-6-9</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
@Controller
public class MessageGroupController {

	@Resource(name = "groupServiceImpl")
	private GroupService groupService;

	/******************************* 短信分组 ***********************************/
	/** 
	 * 分组分页列表
	 * @return
	 */
	@RequestMapping("/showGroupList.do")
	public String showAllList(HttpServletRequest req) {
		String pageNoStr = req.getParameter("pageNo");
		String groupId = req.getParameter("groupId");
		int pageNo = 1;
		PaginatedResult result;
		Map<String, Object> map = new HashMap<String, Object>();
		if(pageNoStr != null){
			pageNo = Integer.parseInt(pageNoStr);
		}
		if(groupId != null){
			map.put("groupId", groupId);
			result = this.groupService.getGroupPageList(pageNo, Global.PAGESIZE, map);
		}else{
			result = this.groupService.getGroupPageList(pageNo, Global.PAGESIZE, null);
		}
		req.setAttribute("result", result);		
		req.setAttribute("groupId", groupId);
		return "message/messageGroup";
	}
	
	/**
	 * 根据标识查询分组列表
	 * @return
	 */
	@RequestMapping("/showGroupListById.do")
	public String showAllListById(HttpServletRequest req) {
		String groupId = req.getParameter("groupId");
		List<Group> list = groupService.getGroupList(Integer.parseInt(groupId));
		req.setAttribute("groupList", list);
		return "message/messageGroup";
	}
	
	/**
	 * 根据标识编辑分组信息
	 * @return
	 */
	@RequestMapping("/editGroup.do")
	public String editGroupInfoById(HttpServletRequest req) {
		String groupId = req.getParameter("groupId");
		List<Group> list = groupService.getGroupList(Integer.parseInt(groupId));
		Group group = list.get(0);
		req.setAttribute("group", group);
		return "message/editGroup";
	}
	
	/**
	 * 更新分组信息
	 * @return
	 */
	@RequestMapping("/updGroup.do")
	public String updGroupInfoById(@ModelAttribute("group") Group group, HttpServletRequest req,
			HttpServletResponse res) {
		String groupId = req.getParameter("groupId");
		Toolkit tool = new Toolkit();
		//设置分组时间
		group.setCreateTime(tool.getSystemDate());
		group.setGroupId(Integer.parseInt(groupId));
		int num = groupService.updGroup(group);
		if(num>0){
			req.setAttribute("info", "更新成功");
		} else {
			req.setAttribute("info", "更新失败");
		}
		return "message/messageGroup";
	}
	
	/**
	 * 根据标识删除分组信息
	 * @return
	 */
	@RequestMapping("/delGroup.do")
	public String delGroupById(HttpServletRequest req) {
		String groupId = req.getParameter("groupId");
		int num = groupService.delGroup(Integer.parseInt(groupId));
		if(num>0){
			req.setAttribute("info", "删除成功");
		} else {
			req.setAttribute("info", "删除失败");
		}
		return "message/messageGroup";
	}
	
	/******************************* 新建分组 ***********************************/
	/**
	 * 新建分组基本信息
	 * @return
	 */
	@RequestMapping("/newGroup.do")
	public String newGroup() {
		return "message/newGroup";
	}
	
	/**
	 * 保存分组基本信息
	 * @return
	 */
	@RequestMapping("/insGroup.do")
	public String insGroup(@ModelAttribute("group") Group group, HttpServletRequest req,
			HttpServletResponse res) {
		Toolkit tool = new Toolkit();
		//设置分组时间
		group.setCreateTime(tool.getSystemTime());
		group.setUserId(((MpUser)SpringSecurityUtils.getCurrentUser()).getUserId());
		int num = groupService.insertGroup(group);
		if(num>0){
			req.setAttribute("info", "录入成功");
		} else {
			req.setAttribute("info", "录入失败");
		}
		return "message/messageGroup";
	}

}
